package cn.edu.swu.miniwebsvr.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//统一的HTTP请求数据模型
//存储解析后的请求方法、URL、头部信息和参数
public class Parameter {
    private String method;// HTTP请求方法：GET、POST等
    private String url;// 请求的URL路径
    private Map<String, String> urlParams = new HashMap<>();// URL查询参数（GET参数）
    private Map<String, String> bodyParams = new HashMap<>();// 请求体参数（POST参数）
    private Map<String, String> headers = new HashMap<>();// HTTP请求头信息

    // TODO: [222024321072057/王文薏] 补充必要的构造函数、setter、getter方法
    public Parameter() {}//无参构造函数
    public Parameter(String method, String url, Map<String, String> urlParams,
                     Map<String, String> bodyParams, Map<String, String> headers) {
        this.method = method;
        this.url = url;
        this.urlParams.putAll(urlParams); // 避免外部修改内部Map，使用复制
        this.bodyParams.putAll(bodyParams);
        this.headers.putAll(headers);
    }

    //获取所有参数的合并结果（URL参数 + 请求体参数）
    //@return 不可修改的合并参数Map
    public Map<String, String> getAllParams() {
        Map<String, String> allParams = new HashMap<>();
        allParams.putAll(urlParams);
        allParams.putAll(bodyParams);
        return Collections.unmodifiableMap(allParams);
    }

    //统一的参数访问方法,根据参数名获取参数值 - 优先从请求体参数中获取，其次从URL参数中获取
    public String getParam(String name) {
        if (bodyParams.containsKey(name)) {
            return bodyParams.get(name);
        }
        return urlParams.get(name);
    }

    // 示例 Getter/Setter，请补全
    public void setMethod(String method) {
        this.method = method;
    }

    public String getMethod() {
        return this.method;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return this.url;
    }

    public Map<String, String> getUrlParams() {
        return urlParams;
    }

    public void setUrlParams(Map<String, String> urlParams) {
        this.urlParams = urlParams;
    }

    public Map<String, String> getBodyParams() {
        return bodyParams;
    }

    public void setBodyParams(Map<String, String> bodyParams) {
        this.bodyParams = bodyParams;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public String getHeader(String s) {
        return s;
    }
}
